﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.Office.Workflow.Utility;

namespace SharePointWorkflows.ConfigureItemLevelPermissions
{
    public sealed partial class Workflow : SequentialWorkflowActivity
    {
        public Workflow()
        {
            InitializeComponent();
        }

        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

        Guid webId;
        Guid siteId;
        Guid listId;
        int itemId;

        private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
        {
            //set member variable values from
            //the inbound list context
            webId = workflowProperties.WebId;
            siteId = workflowProperties.SiteId;
            listId = workflowProperties.ListId;
            itemId = workflowProperties.ItemId;

        }

        private SPUser GetUser(SPListItem item, SPField userField)
        {

            string currentValue = item[userField.Title].ToString();
            SPFieldUser field = (SPFieldUser)userField;
            SPFieldUserValue fieldValue = (SPFieldUserValue)field.GetFieldValue(currentValue);
            return fieldValue.User;

        }

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            //all the id's for the site, current list and item
            SPSite site = new SPSite(siteId);
            SPWeb web = site.OpenWeb(webId);
            SPList list = web.Lists[listId];
            SPListItem listItem = list.GetItemById(itemId);

            //break from parent permissions
            listItem.BreakRoleInheritance(false);

            //delete any existing permissions in the
            //case that this is an update to an item
            SPRoleAssignmentCollection currentRoles =
                listItem.RoleAssignments;
            foreach (SPRoleAssignment role in currentRoles)
            {
                role.RoleDefinitionBindings.RemoveAll();
                role.Update();
            }

            
            string username = GetUser(listItem, listItem.Fields["Created By"]).LoginName;

            //create"contributor" role
            SPRoleDefinition roleDef =
             web.RoleDefinitions.GetByType(SPRoleType.Contributor);
            SPRoleAssignment roleAssignment =
             new SPRoleAssignment(
                 username,
                 string.Empty,
                 string.Empty,
                 string.Empty);
            roleAssignment.RoleDefinitionBindings.Add(roleDef);

            //update list item with new assignment
            currentRoles.Add(roleAssignment);
            listItem.SystemUpdate();

        }
    }
}
